#include "dump.h"
#define MAX_FILE_PATH_LEN 256

ui8  file_path[ MAX_FILE_PATH_LEN ];
ui8  file_out [  MAX_FILE_PATH_LEN ];
ui32 help_me   = 0;
ui32 option    = 0;
ui8 *osymbol   = NULL;
ui8 *dsymbol   = NULL;
ui32 option_index = 0;

void dump_set_options(ui32 argc, ui8 **argv){
	ui32 m,n,l,x,ch;                               
	ui8 buffer[256];                          

	for( n = 1; n < argc; n++ )           
	{
		switch( (ui32)argv[n][0] )           
		{
		case '-':
		case '/': x = 0;                   /*Bail out if 1. */
			l = strlen( argv[n] );
			for( m = 1; m < l; ++m ) /*Scan through options. */
			{
				ch = (ui32)argv[n][m];
				switch( ch )
				{
					case 'o':
					case 'O':
						if( m + 1 >= l )
						{
							puts( "Illegal syntax -- no string!" );
							exit( 1 );
						}
						else
						{
							strcpy( buffer, &argv[n][m+1] );
						}
						x = 1;

						option = atoi(buffer, 10);

						option_index = n;

						break;
					case 'h':
					case 'H':
						help_me = 1;
						break;
					case 'b':
					case 'B':if( m + 1 >= l )
							  {
								  puts( "Illegal syntax -- no string!" );
								  exit( 1 );
							  }
							  else
							  {
								  strcpy( file_out, &argv[n][m+1] );
							  }
							  x = 1;
							  break;					
					case 'f':              /*String parameter. */
					case 'F': if( m + 1 >= l )
							  {
								  puts( "Illegal syntax -- no string!" );
								  exit( 1 );
							  }
							  else
							  {
								  strcpy( file_path, &argv[n][m+1] );
							  }
							  x = 1;
							  break;
					case 'c':
					case 'C':if( m + 1 >= l )
							  {
								  puts( "Illegal syntax -- no string!" );
								  exit( 1 );
							  }
							  else
							  {
								  osymbol = &argv[n][m+1];
							  }
							  x = 1;
							  break;					
					case 'd':
					case 'D':if( m + 1 >= l )
							  {
								  puts( "Illegal syntax -- no string!" );
								  exit( 1 );
							  }
							  else
							  {
								  dsymbol = &argv[n][m+1];
							  }
							  x = 1;
							  break;					
					default:  
						printf( "Illegal option code = %c\n", ch );
						x = 1;      /*Not legal option. */
						exit( 1 );
						break;
				}
				if( x == 1 )
				{
					break;
				}
			}
			break;
		default:  /*Not option -- text. */
			break;
		}
	}
}

void do_helpme(){
	printf("\n\n");
	printf("options: \n");
	printf("         Group 1 \n");
    printf("           dump /O1 /fdump.obj\n");
    printf("         Group 2 \n");
	printf("           dump /O2 /fdump.obj /btest_class.c\n"); 
	printf("         Group 3 \n");
	printf("           dump /O3 /fdump.obj /csymbol /dsymbol\n"); 
	printf("         Group 4 \n");
	printf("           dump /O4 dump.obj dumpcls.obj header.obj relations.obj relocations.obj section.obj stringtable.obj symbol.obj xml.obj\n");
    printf("         Other - XML Format\n");
	printf("           dump /o5 /fdump.obj \n");
	printf("\n\n\n\n");
	printf("Execute: dump /h ");
}

ui32 main(ui32 argc, ui8 **argv){
	printf("\nISEL - Instituto de Engenharia de Lisboa\n");
	printf("           PICC - Prog. Imp. C/C++\n");
	printf("               dump coff tool \n");

	dump_set_options(argc, argv);

    //render program switch
	if( help_me || argc == 1){
		do_helpme();

		if(argc == 1){
			return EXIT_SUCCESS;
		}
	}

	dump_cls_constructor( &file_path );
		switch( option ){
			case FASE1:
				do_option_1();
				break;
			case FASE2:
				do_option_2(file_out);
				break;
			case FASE3:
				do_option_3(osymbol, dsymbol);
				break;
			case FASE4:
				do_option_4(argc, option_index, argv);
				break;
			default:
				dumpcls_show_header();
				dumpcls_show_section();
				dumpcls_show_symbol();		
				dumpcls_close_header();
		}
	dump_cls_destructor();
}